package gui;

import java.util.EnumSet;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import enums.DayEnum;

/**
 * @author markus
 *
 */
public class WeeklyPatternComposite extends Composite {

	/**
	 * checkButtons for weekdays.
	 */
	private Button[] checkButtons;
	/**
	 * @param parent parent
	 * @param style style
	 */
	public WeeklyPatternComposite(final Composite parent, final int style) {
		super(parent, style);
		GridLayout layout = new GridLayout(4, true);
		this.setLayout(layout);
		this.checkButtons = new Button[7];
		
		for (int i = 0; i < 7; ++i) {
			checkButtons[i] = new Button(this, SWT.CHECK);
			checkButtons[i].setText(DayEnum.values()[i].toString().substring(0, 3));
			checkButtons[i].setLayoutData(new GridData());
		}
	}
	
	/**
	 * @return the weekDay of this control
	 */
	public EnumSet<DayEnum> getWeekDay() {
		EnumSet<DayEnum> result = EnumSet.noneOf(DayEnum.class);
		for (int i = 0; i < 7; ++i) {
			if (checkButtons[i].getSelection()) {
				result.add(DayEnum.values()[i]);
			}
		}
		return result;
	}
	
	/**
	 * @param weekDay input weekDay
	 */
	public void setWeekDay(final EnumSet<DayEnum> weekDay) {
		for (int i = 0; i < 7; ++i) {
			if (weekDay.contains(DayEnum.values()[i])) {
				checkButtons[i].setSelection(true);
			} else {
				checkButtons[i].setSelection(false);
			}
		}
	}
	
	@Override
	public void setEnabled(final boolean b) {
		for (int i = 0; i < 7; ++i) {
			this.checkButtons[i].setEnabled(b);
		}
	}
	
	/**
	 * @return verification
	 */
	public boolean verify() {
		for (int i = 0; i < 7; ++i) {
			if (this.checkButtons[i].getSelection()) {
				return true;
			}
		}
		return false;
	}
}
